package cn.gz.demo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.bean.FyBean;
import cn.gz.demo.databinding.FyBinding;
import cn.gz.demo.listener.OnViewItemListener;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Lyz on 2017/3/8.
 */
public class FyActivity extends BaseActivity implements OnViewItemListener {
    private FyBinding binding;
    private FyAdapter adapter;
    private Activity context;
    private List<FyBean> list = new ArrayList<>();
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_fy);

        context = this;
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        adapter = new FyAdapter(this);
        binding.recyclerview.setAdapter(adapter);
        binding.recyclerview.setNestedScrollingEnabled(false);

        for (int i = 0; i < 50; i++) {
            list.add(new FyBean((i + 1) + ""));
        }
        list.get(0).setCheck(true);
        adapter.notifyDataSetChanged();

        binding.upper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index > 0) {
                    --index;
                    binding.recyclerview.smoothScrollToPosition(index);
                    onItemClick(index);
                }
            }
        });
        binding.lower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index < (list.size() - 1)) {
                    ++index;
                    binding.recyclerview.scrollToPosition(index);
                    onItemClick(index);
                }
            }
        });
    }

    @Override
    public void onItemClick(int position) {
        index = position;
        for (int i = 0; i < list.size(); i++) {
            if (i == position) {
                list.get(i).setCheck(true);
            } else {
                list.get(i).setCheck(false);
            }
        }
        adapter.notifyDataSetChanged();
    }

    class FyAdapter extends RecyclerView.Adapter<FyAdapter.MyViewHolder> {
        OnViewItemListener listener;

        public FyAdapter(OnViewItemListener listener) {
            this.listener = listener;
        }

        @Override
        public FyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.main_fy_item, parent, false);
            return new FyAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final FyAdapter.MyViewHolder holder, final int position) {
            final FyBean bean = list.get(position);
            holder.tv.setText(bean.getContent());

            if (bean.isCheck()) {
                holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                holder.tv.setBackgroundResource(R.color.red);
            } else {
                holder.tv.setTextColor(context.getResources().getColor(R.color.black));
                holder.tv.setBackgroundResource(R.color.white);
            }

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            private TextView tv;

            public MyViewHolder(View view) {
                super(view);
                tv = (TextView) view.findViewById(R.id.tv);
            }
        }
    }
}
